/*
 * BackupGUI.java
 *
 * Created on March 27, 2007, 5:01 PM
 */

package edu.byu.isys413.jjenkins.view;

import edu.byu.isys413.jjenkins.data.*;
import java.sql.Date;
import javax.swing.JOptionPane;

/**
 * This GUI is called from the New order gui to start a backup transaction
 * @author byu
 */
public class BackupGUI extends javax.swing.JFrame {
    BackupBO backup;
    /** Creates new form BackupGUI */
    private BackupGUI() {
        initComponents();
        pack();
    }
        /*This mehod ensures that only one instance of revenueSourceFactory
         *exists */
    private static BackupGUI instance = null;
    
    /**
     * gets the instance
     */
    public static synchronized BackupGUI getInstance() {
        if (instance == null) {
            instance = new BackupGUI();
        }
        return instance;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbSize = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cmbLength = new javax.swing.JComboBox();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.FlowLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Backup Service");
        jPanel2.setPreferredSize(new java.awt.Dimension(300, 140));
        jLabel2.setText("Select Backup Details");
        jLabel2.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 0, 0)));
        jLabel2.setPreferredSize(new java.awt.Dimension(250, 15));
        jPanel2.add(jLabel2);

        jPanel1.setLayout(new java.awt.GridLayout(3, 2, 10, 10));

        jPanel1.setAutoscrolls(true);
        jPanel1.setPreferredSize(new java.awt.Dimension(250, 90));
        jLabel1.setText("Backup Size (GB):     ");
        jLabel1.setPreferredSize(new java.awt.Dimension(120, 14));
        jPanel1.add(jLabel1);

        cmbSize.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "10", "20", "30", "60", "120" }));
        jPanel1.add(cmbSize);

        jLabel3.setText("Length (in months):");
        jLabel3.setPreferredSize(new java.awt.Dimension(120, 14));
        jPanel1.add(jLabel3);

        cmbLength.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "6", "12", "18", "24", "36" }));
        jPanel1.add(cmbLength);

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        jPanel1.add(btnSave);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jPanel1.add(btnCancel);

        jPanel2.add(jPanel1);

        getContentPane().add(jPanel2);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed
    
    /**
     * this method is called to populate
     */
    public void populate(BackupBO backup){
        try{
            this.backup = backup;
            backup.setBeginDate(new Date(System.currentTimeMillis()));
            
            // Set and create conceptual coupon. This should be already in the database and we should see what is the most recent
            ConceptualBackupBO backupBO = ConceptualBackupDAO.getInstance().create();
            backupBO.setPrice(1);
            ConceptualBackupDAO.getInstance().save(backupBO);
            backup.setConceptualBackup(backupBO);
            //****************************************
            
            
            this.setVisible(true);
            
        }catch(DataException ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, ex.toString(),"Cannot Create Backeup", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        
        backup.setBackupSize(Double.parseDouble(cmbSize.getSelectedItem().toString()));
        backup.setEndDate(backup.addMonths(backup.getBeginDate(), Double.parseDouble(cmbLength.getSelectedItem().toString())));
        backup.setDescription(backup.createDescription());
        backup.setPrice(backup.calcPrice());

        try {
            
            RevenueSourceFactory.getInstance().getDAO("backup").save(backup);
        } catch (DataException ex) {
             JOptionPane.showMessageDialog(null, ex.toString(),"Cannot Save Backup", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
        NewOrder.getInstance().populateTransactionLines();
        this.dispose();
        
        
       
    }//GEN-LAST:event_btnSaveActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BackupGUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cmbLength;
    private javax.swing.JComboBox cmbSize;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
    
}
